
/*search*/
$(function(){
    //搜索提交
    $(document).on('click',"#search",function(){
    	var text = $("#search-input").val();
    	if(!$.trim(text)){
    		lay("请输入搜索内容！");
    		return false;
    	}
    	document.getElementById("searchform").action="/home/search?q="+ text;
    	$("#searchform").submit();
    });
    //我的提问跳转cookie验证
    $(document).on('click',"#myask",function(){
    	if($.cookie("isLogin") == 0){
    		login_fn("/user/myquestions?i=myask");
    	} else{
    		location.assign("/user/myquestions?i=myask");
    	}
    });
    $(document).on('click',"#serclick",function(){
    	if($.cookie("isLogin") == 0){
    		login_fn("/user/askquestion?i=myask");
    	} else{
    		location.assign("/user/askquestion?i=myask");
    	}
    });
    /*textarea count*/
    var maxCount = 200;  // 最高字数，这个值可以自己配置
    $("#js_questionContent").on('keyup', function() {
    	var len = this.value.length;
    	var ll = maxCount-len < 0 ? 0 : maxCount-len
    	$("#letnumber").html(ll);
    });
})

/***** paging ****/
function generatePaginate(containerId, urlTemplate, page, total, size, limit) {
	var container = $("#" + containerId);
    container.html(""); //clear container
    var page = parseInt(page);
    var totalPage = Math.ceil(total / size);
    var start = 1;
    var end = totalPage;
    if (total == 0){container.css("display","none");};
    if(limit){//@todo
    	var start = page-limit;
    	var end = page+limit;
    	start = Math.max(start, 1);
    	end = Math.min(end, totalPage);
    	if(start == 1) end = Math.max(end, limit*2);
    	if(end == totalPage) start = Math.min(start, totalPage-limit*2);
    	start = Math.max(start, 1);
    	end = Math.min(end, totalPage);
    }
    for (var i = start; i <= end; i++) {
    	var li = (page == i) ? ('<li class="active"><a>' + (i) + '</a></li>') : ('<li><a href="' + urlTemplate.replace('#[page]', i) + '">' + (i) + '</a></li>');
    	$(li).appendTo(container);
    }
    var prev = (page > start) ? ('<li class="prev"><a href="' + urlTemplate.replace('#[page]', page - 1) + '">&lt;</a></li>') : ('<li class="prev disabled"><a>&lt;</a></li>');
    $(prev).prependTo(container);
    var next = (page < end) ? ('<li class="next"><a href="' + urlTemplate.replace('#[page]', page + 1) + '">&gt;</a></li>') : ('<li class="next disabled"><a>&gt;</a></li>');
    $(next).appendTo(container);
}
/***** paging end****/

/***** paging-ex 用户问题编辑页分页****/
function generatePaginateex(containerId, urlTemplate, page, total, size, limit) {
	var container = $("#" + containerId);
    container.html(""); //clear container
    var page = parseInt(page);
    var totalPage = Math.ceil(total / size);
    var start = 1;
    var end = totalPage;
    if (total == 0){container.css("display","none");};
    if(limit){//@todo
    	var start = page-limit;
    	var end = page+limit;
    	start = Math.max(start, 1);
    	end = Math.min(end, totalPage);
    	if(start == 1) end = Math.max(end, limit*2);
    	if(end == totalPage) start = Math.min(start, totalPage-limit*2);
    	start = Math.max(start, 1);
    	end = Math.min(end, totalPage);
    }
    for (var i = start; i <= end; i++) {
    	var li = (page == i) ? ('<li class="active"><a>' + (i) + '</a></li>') : ('<li><a href="' + urlTemplate.replace('#[page]', i) + '">' + (i) + '</a></li>');
    	$(li).appendTo(container);
    }
    var prev = (page > start) ? ('<li class="prev"><a href="' + urlTemplate.replace('#[page]', page - 1) + '">上一页</a></li>') : ('<li class="prev disabled"><a>上一页</a></li>');
    $(prev).appendTo(container);
    var next = (page < end) ? ('<li class="next"><a href="' + urlTemplate.replace('#[page]', page + 1) + '">下一页</a></li>') : ('<li class="next disabled"><a>下一页</a></li>');
    $(next).appendTo(container);
}
/***** paging-ex end****/

/*********** lay out start ***************/
function lay(msg){
	var html = '<div class="lay-wrap"></div>'+
	'<div class="lay-box" style="display:block;">'+
	'<div class="lay-header">提示信息</div>'+
	'<div class="lay-body">' + msg + '</div>'+
	'<div class="lay-footer">确认</div>'+
	'</div>'
	$('body').append(html);
	$(document).on('click','.lay-footer',function(){
		$(this).parent().hide();
		$('.lay-wrap').remove();
		$('.lay-box').remove();
	})
}

/*************** lay out end ***************/

/*left menu*/
    /*$(document).on("click",".artop",function(){
       $(this).removeClass("artop").addClass("ardown");
       $(this).parent().removeClass("slip");
       $(this).parent().next(".list").slideUp("fast");
   });
    $(document).on("click",".ardown",function(){
       $("aside .artop").trigger("click");
       $(this).removeClass("ardown").addClass("artop");
       $(this).parent().addClass("slip");
       $(this).parent().next(".list").slideDown("fast");

   });*/
    //open the default menu
    //$("aside li[id=pid] .ardown").trigger("click");
    

    /******ie8兼容placeholder********/
   var JPlaceHolder = {
    //检测
    _check : function(){
        return 'placeholder' in document.createElement('input');
    },
    //初始化
    init : function(){
        if(!this._check()){
            this.fix();
        }
    },
    //修复
    fix : function(){
        jQuery(':input[placeholder]').each(function(index, element) {
            var self = $(this), txt = self.attr('placeholder');
            self.wrap($('<div></div>').css({position:'relative', zoom:'1', border:'none', background:'none', padding:'none', margin:'none'}));
            var pos = self.position(), h = self.outerHeight(true)+'px', paddingleft = self.css('padding-left');
            var holder = $('<span></span>').text(txt).css({position:'absolute', left:pos.left, top:pos.top, height:h, lineHeight:h, paddingLeft:paddingleft, color:'#aaa'}).appendTo(self.parent());
            if(self.val()){
                    holder.hide();
                };
            self.focusin(function(e) {
                holder.hide();
            }).focusout(function(e) {
                if(!self.val()){
                    holder.show();
                }
            });
            holder.click(function(e) {
                holder.hide();
                self.focus();
            });
        });
    }
};
//执行
jQuery(function(){
    JPlaceHolder.init();    
});
    /******ie8兼容placeholder over********/ 